/*
 * MailUtil.java
 *
 * Created on July 26, 2006, 9:22 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.asaga.tools;

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;


/**
 * 
 * Mail Utility Objec
 * used to send emails
 * @author mmixon -- created by Michael Mixon
 */
public class MailUtil {
    
    /** Creates a new instance of MailUtil */
    public MailUtil() {
    }
    
    /**
     * Send Mail Method
     * @param to to field in email 
     * @param from -- from field in email
     * @param subject -- subject line in email
     * @param messageText -- text of email
     * @throws javax.mail.MessagingException -- thrown when error occurs
     */
    public static void sendMail(String to, String from, String subject,
            String messageText) throws MessagingException{
        
        //get mail session
        Properties props = new Properties();
        props.put("mail.smtp.host", "email.uga.edu");
        Session session = Session.getDefaultInstance(props);
        
        //create a message
        MimeMessage message = new MimeMessage(session);
        message.setSubject(subject);
        message.setText(messageText);
        
        //address the message
        InternetAddress fromAddress = new InternetAddress(from);
        InternetAddress toAddress = new InternetAddress(to);
        message.setFrom(fromAddress);
        message.setRecipient(Message.RecipientType.TO, toAddress);
        
        //send message
        Transport.send(message);
    }
    private Session getSession() {
        Authenticator authenticator = new Authenticator();
        
        Properties properties = new Properties();
        properties.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
        properties.setProperty("mail.smtp.auth", "true");
        
        properties.setProperty("mail.smtp.host", "mail.example.com");
        properties.setProperty("mail.smtp.port", "25");
        
        return Session.getInstance(properties, authenticator);
    }
    
    private class Authenticator extends javax.mail.Authenticator {
        private PasswordAuthentication authentication;
        
        public Authenticator() {
            String username = "mist7590@gmail.com";
            String password = "autism";
            authentication = new PasswordAuthentication(username, password);
        }
        
        protected PasswordAuthentication getPasswordAuthentication() {
            return authentication;
        }
    }
    
    /*public static void main(String[] args){
        try{
           MailUtil emailMessage = new MailUtil();
           String message = "you have a new message";
           String fromAddr = "mmixon@uga.edu";
           String toAddr = "mmixon@uga.edu";
           String subject = "you have a new request";
           emailMessage.sendMail(toAddr, fromAddr, subject, message);
           System.out.println("Success!  you've got mail! \n");
        }
        catch(MessagingException me){
            me.printStackTrace();
        }
    }*/
    
}
